

<script>
/*// 1
const v=new Promise(resolve=>{
    resolve(3)
})
v.then((res)=>{
    console.log(res);
})
// 2
const v1=new Promise((undefined,reject)=>{
    reject('嘿')
})
v1.then((res)=>{
    console.log(res);
})
v1.catch((e)=>{
    console.log(e);
})

// 3
function makePromise(flag,value){
    return new Promise((resolve,reject)=>{
      if(flag==true){
          resolve(value)
      }else{
          reject(value)
      }
    })
}

makePromise(true,1).then( (res)=>{console.log(res)})//->成功promise，值=1
makePromise(false,2).catch((e)=>{console.log(e);})
//->失败promise,值=2
*/
// 4
function makeDelayPromise(ms,flag,value){
return new Promise((resolve,reject)=>{
    setTimeout(()=>{
        if(flag==true){
          resolve(value)
      }else{
          reject(value)
      }
    },1000)
})
}
//async写法：
// async function makeDelayPromise(ms,flag,value){
//     const delay=()=>new Promise((resolve,reject)=>{
//         setTimeout(resolve,ms)
//     })
//     await delay(ms)
//     if(flag){
//         return value
//     }else{
//         throw value
//     }
// }

makeDelayPromise(1000,true,1).then( (res)=>{console.log(res)})//->promise 1000ms后成功，值=1
makeDelayPromise(1000,false,2).catch((e)=>{console.log(e)})//->promise 1000ms后失败，值=2

// 5
function processDate(data){
    var promise=new Promise(function(resolve,reject){
        setTimeout(function(){
            resolve(data.value)
        },Math.random()*1000+500)
    })
    return promise
}
var data=[
    {value:'奔'},
    {value:'啵'},
    {value:'霸儿'}
]
var processed=[]
data.forEach(function(elem){
    processed.push(processDate(elem))
})
Promise.all(processed).then(function(response){
    console.log(response);// ['奔', '啵', '霸儿']
})

</script>